package pt.iscte.pcd.project.client;

import pt.iscte.pcd.project.order.AddDirectoryOrder;
import pt.iscte.pcd.project.order.AddFileOrder;
import pt.iscte.pcd.project.order.ListFilesTableOrder;
import pt.iscte.pcd.project.order.Order;
import pt.iscte.pcd.project.order.ReadFileOrder;
import pt.iscte.pcd.project.order.WriteFileOrder;
import pt.iscte.pcd.project.remote.RemoteDirectory;
import pt.iscte.pcd.project.remote.RemoteFile;

public class ClientWorker extends Thread {

	private DealWithServer server;

	public ClientWorker(DealWithServer server) {
		this.server = server;
	}
	
	@Override
	public void run() {
		Order order = null;
		try {
			order = server.retiraPedidosDaListaRecebidos();
			switch (order.getTypeOrder()) {
			case LIST_FILES_TABLE:
				server.getGUI().createModel(((ListFilesTableOrder)order).getDirectory());
				break;

			case ADD_FILE_ORDER:
				server.getGUI().updateModel((RemoteDirectory)((AddFileOrder)order).getEntry());
				break;
				
			case ADD_DIRECTORY_ORDER:
				server.getGUI().updateModel(((AddDirectoryOrder)order).getRoot());
				break;
			
			
			case READ_FILE_ORDER:
				server.getGUI().readFile((RemoteFile) ((ReadFileOrder) order).getEntry());
				break;
				
			case WRITE_FILE_ORDER:
				server.getGUI().writeFile((RemoteFile)((WriteFileOrder)order).getEntry());
				break;
				
			default:
				break;
			
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
	}
}
